<?php
class UserPhotoController extends Zone_Action{
	public function indexAction(){
		$this->removeLayout();
		$user_id = get_user_id();
		$user = $this->Model->fetchRow("SELECT `photo` FROM `users` WHERE `ID`='$user_id'"); 
		$this->set('photo',$user['photo']);
		
		$fields = array(
			photo	=> array(
				type			=> 'IMAGE',
				path			=> 'files/photo/',
				resize			=> array(95,95),
				max_size		=> 1024,
				return_name		=> true	
			)	
		);
		
		loadClass('ZData');
		$f = new ZData();
		$f->addField($fields);
		if( isPost() ){
			$data = $f->getData();
			if(!is_array( $data )){
				return $this->setJSON(array(
					alert	=> $data
				));
			}
		
			$this->Model->update("users",$data,"`ID`='$user_id'");
			if( $user['photo'] ){
				@unlink("files/photo/{$user['photo']}");
			}	
			return $this->setJSON(array(
				callback => "(function(){
					$('#uphoto')
						.each(function(){
							this.src = baseURL+'/files/photo/{$data['photo']}?'+(new Date()).getTime();
						})
				})()"
			));			
		}else{
			die('200');
		}
	}
}	